<template>
  <div>
    <el-button type="text"
               @click="dialogFormVisible = true">上传宿舍检查记录</el-button>
    <el-dialog title="上传当前宿舍检查信息"
               width="20%"
               :visible.sync="dialogFormVisible">
      <el-form ref="form"
               :rules="rules"
               :model="form"
               label-position="right"
               size="small">
        <el-form-item label="卫生"
                      prop="hygiene">
          <el-input v-model="form.hygiene"></el-input>
        </el-form-item>
        <el-form-item label="文化"
                      prop="culture">
          <el-input v-model="form.culture"></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="date"
                          type="date"
                          style="width: 168px;height: 30px"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea"
                    v-model="form.desc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="inspectionUp">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'inspector',
  props: {
    dormInfo: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    var scorecheck = (rule, value, callback) => {
      const num = new RegExp('[0-9]')
      if (num.test(value)) {
        value = Number(value)
        if (value && (value < 0 || value > 10)) {
          return callback(new Error('请输入分值：0-10'))
        }
      } else {
        return callback(new Error('请输入0-10之间的分值'))
      }
    }
    return {
      dialogFormVisible: false,
      date: '',
      form: {
        hygiene: '',
        culture: '',
        time: '',
        desc: ''
      },
      rules: {
        hygiene: [
          { message: '请输入分值：0-10', trigger: 'blur' },
          { validator: scorecheck }
        ],
        culture: [
          { trigger: 'blur', message: '请输入分值：0-10' },
          { validator: scorecheck }
        ]
      }
    }
  },

  methods: {
    // 卫生记录表上传
    async inspectionUp () {
      const score = (Number(this.form.hygiene) + Number(this.form.culture)) / 4
      this.dialogFormVisible = false
      const { data: res } = await this.$axios.get('/inspect/inspectionUp', {
        params: {
          inspector: window.localStorage.getItem('name'),
          tower: this.dormInfo.tower,
          dorm: this.dormInfo.dorm,
          class: this.dormInfo.class,
          hygiene: this.form.hygiene,
          culture: this.form.culture,
          score: String(score),
          desc: this.form.desc,
          time: this.date
        }
      })
      console.log(res)
      if (res.meta.code !== -1) {
        this.form = {
          hygiene: '',
          culture: '',
          time: '',
          desc: ''
        }
        // 同时将最新修改的score上传给宿舍信息表
        const { data: resp } = await this.$axios.get('/student/newScore', {
          params: {
            tower: this.dormInfo.tower,
            dorm: this.dormInfo.dorm,
            score: String(score)
          }
        })
        // console.log(resp)
        if (resp.meta.code !== -1) {
          const message = res.meta.msg + '，查看更新后的数据请自行刷新当前页面'
          this.$message.success(message)
          // location.reload()
        }
      } else {
        this.$message.error(res.meta.msg)
        this.form = {
          hygiene: '',
          culture: '',
          time: '',
          desc: ''
        }
      }
    }
  }

}

</script>
<style  scoped>
</style>
